/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.basics

import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.dsl.FXGL.Companion.entityBuilder
import com.almasb.fxgl.entity.*
import com.almasb.fxgl.input.*
import javafx.scene.input.*
import javafx.scene.paint.*
import javafx.scene.shape.*

/**
 * Shows how to use input service and bind actions to triggers.
 */
class InputSample : GameApplication() {
	private lateinit var player: Entity
	
	override fun initSettings(settings: GameSettings) {}
	
	override fun initGame() {
		player = entityBuilder()
			.at(150.0, 150.0) //指定生成的位置
			.view(Rectangle(40.0, 40.0, Color.BLUE)) //指定生成的视图大小和形状
			.buildAndAttach() //构建视图并添加到游戏世界
	}
	
	override fun initInput() {
		// 1. get input service
		
		val input = getInput()
		
		// 2. add key/mouse bound actions
		// when app is running press F to see output to console
		
		input.addAction(object : UserAction("Move Up") {
			override fun onAction() {
				player.translateY(5.0) // move right 5 pixels
			}
		}, KeyCode.W)
		
		input.addAction(object : UserAction("Move Down") {
			override fun onAction() {
				player.translateY(-5.0) // move right 5 pixels
			}
		}, KeyCode.S)
		
		input.addAction(object : UserAction("Move Left") {
			override fun onAction() {
				player.translateX(-5.0) // move right 5 pixels
			}
		}, KeyCode.A)
		
		input.addAction(object : UserAction("Move Right") {
			override fun onAction() {
				player.translateY(5.0) // move right 5 pixels
			}
		}, KeyCode.D)
		
		//input.addAction(userAction("Attack"){
		//	onActionBegin {
		//		println("Attack!")
		//	}
		//}, KeyCode.ENTER)
		
		input.addAction(object : UserAction("Print Line") {
			override fun onActionBegin() {
				println("Action Begin")
			}
			
			override fun onAction() {
				println("On Action")
			}
			
			override fun onActionEnd() {
				println("Action End")
			}
		}, KeyCode.F)
	}
}


fun main(args: Array<String>) {
	GameApplication.launch(InputSample::class.java, args)
}

